aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/[lang=lang]/+page.server.ts
blob: deafae398eae9bc4f12c02d5ac1840a38fadbe17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { sanity } from "$lib/sanity/client";
import type { PageServerLoad } from "./$types";
import groq from "groq";
import type { ContactModel } from "./sections/contact.svelte";
import type { HeroModel } from "./sections/hero.svelte";
import type { DescriptionModel } from "./sections/description.svelte";
import type { s } from "sanity-typed-schema-builder";
import type contactType from "$lib/sanity/schemas/default/contact";
import type heroType from "$lib/sanity/schemas/default/hero";
import type descriptionType from "$lib/sanity/schemas/default/description";
import type productType from "$lib/sanity/schemas/default/product";
import type { ProductsModel } from "./sections/products.svelte";

export const load = (async ({ locals }) => {
	const commonParams = {
		lang: locals.locale
	}
	const contactSection: s.infer<typeof contactType> = await sanity.fetch(groq`*[_type == "contact" && __i18n_lang == $lang][0]`, { ...commonParams }) ?? {};
	const heroSection: s.infer<typeof heroType> = await sanity.fetch(groq`*[_type == "hero" && __i18n_lang == $lang][0]`, { ...commonParams }) ?? {};
	const descriptionSection: s.infer<typeof descriptionType> = await sanity.fetch(groq`*[_type == "description" && __i18n_lang == $lang][0]`, { ...commonParams }) ?? {};
	const products: Array<s.infer<typeof productType>> = await sanity.fetch(groq`*[_type == "product" && __i18n_lang == $lang]`, { ...commonParams }) ?? [];

	return {
		contact: {
			phone: contactSection.phone,
			email: contactSection.email,
			phoneHours: contactSection.phoneHours,
			addressLines: contactSection.addressLines?.map((el: string | object) => el) ?? [],
		} as ContactModel,
		hero: {
			title: heroSection.title,
			content: heroSection.content,
		} as HeroModel,
		description: {
			title: descriptionSection.title,
			content: descriptionSection.content,
		} as DescriptionModel,
		products: {
			products: products.map((p) => ({
				cost: p.cost,
				description: p.description,
				duration: p.duration,
				orderLink: p.orderLink,
				title: p.title
			}))
		} as ProductsModel
	};
}) satisfies PageServerLoad;